﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;

namespace DuJianliang.ApplicationPlatform.Proj5715.Process
{
    public class ProcessDal
    {
        public void Dispatch_Insert(DispatchOB dispatchOB)
        {
            string sql = @"INSERT INTO dbo.t5715_Dispatch
                                   ([FlowInstanceID]
                                   ,[DispatchCode]
                                   ,[MissileModel]
                                   ,[RepairType]
                                   ,[PlanCompletionDate]
                                   ,[CreateUser]
                                   ,[CreateDate]
                                   ,[ModifyUser]
                                   ,[ModifyDate]
                                   ,[IssueState]
                                   ,[IssueUser]
                                   ,[IssueDate]
                                   ,[DispatchState]
                                   ,[Remark])
                             VALUES
                                   (@FlowInstanceID
                                   ,@DispatchCode
                                   ,@MissileModel
                                   ,@RepairType
                                   ,@PlanCompletionDate
                                   ,@CreateUser
                                   ,@CreateDate
                                   ,@ModifyUser
                                   ,@ModifyDate
                                   ,@IssueState
                                   ,@IssueUser
                                   ,@IssueDate
                                   ,@DispatchState 
                                   ,@Remark)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, dispatchOB.FlowInstanceID);
                database.AddInParameter(dbCommand, "@DispatchCode", DbType.String, dispatchOB.DispatchCode);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, dispatchOB.MissileModel);
                database.AddInParameter(dbCommand, "@RepairType", DbType.String, dispatchOB.RepairType);
                database.AddInParameter(dbCommand, "@PlanCompletionDate", DbType.String, dispatchOB.PlanCompletionDate);
                database.AddInParameter(dbCommand, "@CreateUser", DbType.String, dispatchOB.CreateUser);
                database.AddInParameter(dbCommand, "@CreateDate", DbType.String, dispatchOB.CreateDate);
                database.AddInParameter(dbCommand, "@ModifyUser", DbType.String, dispatchOB.ModifyUser);
                database.AddInParameter(dbCommand, "@ModifyDate", DbType.String, dispatchOB.ModifyDate);
                database.AddInParameter(dbCommand, "@IssueState", DbType.String, dispatchOB.IssueState);
                database.AddInParameter(dbCommand, "@IssueUser", DbType.String, dispatchOB.IssueUser);
                database.AddInParameter(dbCommand, "@IssueDate", DbType.String, dispatchOB.IssueDate);
                database.AddInParameter(dbCommand, "@DispatchState", DbType.String, dispatchOB.DispatchState);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, dispatchOB.Remark);
                database.ExecuteNonQuery(dbCommand);                
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Dispatch_Delete(string flowInstanceID)
        {
            string sql1 = @"DELETE FROM dbo.t5715_Dispatch WHERE FlowInstanceID = @FlowInstanceID";
            string sql2 = @"DELETE FROM dbo.t5715_DispatchMissile WHERE DispatchID = @FlowInstanceID";

            Database database = DatabaseFactory.CreateDatabase();
            DbConnection dbConnection = null;
            DbTransaction dbTransaction = null;

            try
            {
                dbConnection = database.CreateConnection();
                dbConnection.Open();
                dbTransaction = dbConnection.BeginTransaction();

                DbCommand dbCommand1 = database.GetSqlStringCommand(sql1);
                database.AddInParameter(dbCommand1, "@FlowInstanceID", DbType.String, flowInstanceID);
                database.ExecuteNonQuery(dbCommand1, dbTransaction);

                DbCommand dbCommand2 = database.GetSqlStringCommand(sql2);
                database.AddInParameter(dbCommand2, "@FlowInstanceID", DbType.String, flowInstanceID);
                database.ExecuteNonQuery(dbCommand2, dbTransaction);

                dbTransaction.Commit();
            }
            catch (System.Threading.ThreadAbortException)
            {
                // do nothing. 
                // Helper.ShowMessagePage 方法中使用了 Response.Redirect，该方法会引发 ThreadAbortException 异常
                // 参考http://support.microsoft.com/kb/312629/
            }
            catch (Exception ex)
            {
                if (dbTransaction != null)
                {
                    dbTransaction.Rollback();
                }
                throw ex;
            }
            finally
            {
                if (dbConnection != null && dbConnection.State != ConnectionState.Closed)
                {
                    dbConnection.Close();
                }
            }
        }

//        public void Dispatch_Update(Database database, DispatchOB dispatchOB)
//        {
//            string sql = @"UPDATE dbo.t5715_Dispatch
//                                   (SET [DispatchCode] = @DispatchCode
//                                   ,[MissileModel] = @MissileModel
//                                   ,[RepairType] = @RepairType
//                                   ,[PlanCompletionDate] = @PlanCompletionDate
//                                   ,[ModifyUser] = @ModifyUser
//                                   ,[ModifyDate] = @ModifyDate
//                                   ,[IssueState] = @IssueState
//                                   ,[IssueUser] = @IssueUser
//                                   ,[IssueDate] = @IssueDate
//                                   ,[DispatchState] = @DispatchState
//                                   ,[Remark]) = @Remark) 
//                            WHERE FlowInstanceID = @FlowInstanceID";

//            try
//            {
//                DbCommand dbCommand = database.GetSqlStringCommand(sql);
//                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, dispatchOB.FlowInstanceID);
//                database.AddInParameter(dbCommand, "@DispatchCode", DbType.String, dispatchOB.DispatchCode);
//                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, dispatchOB.MissileModel);
//                database.AddInParameter(dbCommand, "@RepairType", DbType.String, dispatchOB.RepairType);
//                database.AddInParameter(dbCommand, "@PlanCompletionDate", DbType.String, dispatchOB.PlanCompletionDate);
//                database.AddInParameter(dbCommand, "@CreateUser", DbType.String, dispatchOB.CreateUser);
//                database.AddInParameter(dbCommand, "@CreateDate", DbType.String, dispatchOB.CreateDate);
//                database.AddInParameter(dbCommand, "@ModifyUser", DbType.String, dispatchOB.ModifyUser);
//                database.AddInParameter(dbCommand, "@ModifyDate", DbType.String, dispatchOB.ModifyDate);
//                database.AddInParameter(dbCommand, "@IssueState", DbType.String, dispatchOB.IssueState);
//                database.AddInParameter(dbCommand, "@IssueUser", DbType.String, dispatchOB.IssueUser);
//                database.AddInParameter(dbCommand, "@IssueDate", DbType.String, dispatchOB.IssueDate);
//                database.AddInParameter(dbCommand, "@DispatchState", DbType.String, dispatchOB.DispatchState);
//                database.AddInParameter(dbCommand, "@Remark", DbType.String, dispatchOB.Remark);
//                database.ExecuteNonQuery(dbCommand);
//            }
//            catch (Exception ex)
//            {
//                throw ex;
//            }
//        }

        public void Dispatch_UpdateBase(DispatchOB dispatchOB)
        {
            string sql = @"UPDATE dbo.t5715_Dispatch
                                   SET [DispatchCode] = @DispatchCode
                                   ,[MissileModel] = @MissileModel
                                   ,[RepairType] = @RepairType
                                   ,[PlanCompletionDate] = @PlanCompletionDate
                                   ,[ModifyUser] = @ModifyUser
                                   ,[ModifyDate] = @ModifyDate                                  
                                   ,[Remark] = @Remark
                            WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, dispatchOB.FlowInstanceID);
                database.AddInParameter(dbCommand, "@DispatchCode", DbType.String, dispatchOB.DispatchCode);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, dispatchOB.MissileModel);
                database.AddInParameter(dbCommand, "@RepairType", DbType.String, dispatchOB.RepairType);
                database.AddInParameter(dbCommand, "@PlanCompletionDate", DbType.String, dispatchOB.PlanCompletionDate);
                database.AddInParameter(dbCommand, "@ModifyUser", DbType.String, dispatchOB.ModifyUser);
                database.AddInParameter(dbCommand, "@ModifyDate", DbType.String, dispatchOB.ModifyDate);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, dispatchOB.Remark);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void SetDispatchComplete(Database database, string flowInstanceID)
        {
            string sql = @"UPDATE dbo.t5715_Dispatch
                                   SET [DispatchState] = 1
                                   ,[DispatchCompletedDate] = getdate() 
                            WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DbCommand GetDbCommand_Dispatch_Issue(Database database, DispatchOB dispatchOB)
        {
            string sql = @"UPDATE dbo.t5715_Dispatch
                                   SET [IssueState] = @IssueState
                                   ,[IssueUser] = @IssueUser
                                   ,[IssueDate] = @IssueDate
                            WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, dispatchOB.FlowInstanceID);                
                database.AddInParameter(dbCommand, "@IssueState", DbType.String, dispatchOB.IssueState);
                database.AddInParameter(dbCommand, "@IssueUser", DbType.String, dispatchOB.IssueUser);
                database.AddInParameter(dbCommand, "@IssueDate", DbType.String, dispatchOB.IssueDate);

                return dbCommand;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DispatchMissile_Insert(DispatchMissileOB dispatchMissileOB)
        {
            string sql = @"INSERT INTO dbo.t5715_DispatchMissile
                                   ([DispatchMissileID]
                                   ,[DispatchID]
                                   ,[SysNumber]
                                   ,[MissileModel]
                                   ,[MissileCode]
                                   ,[InDate]
                                   ,[RemainingLife]
                                   ,[IsCompleted]
                                   ,[CompletionDate]
                                    )
                             VALUES
                                   (@DispatchMissileID
                                   ,@DispatchID
                                   ,@SysNumber
                                   ,@MissileModel
                                   ,@MissileCode
                                   ,@InDate
                                   ,@RemainingLife
                                   ,@IsCompleted
                                   ,@CompletionDate)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, dispatchMissileOB.DispatchMissileID);
                database.AddInParameter(dbCommand, "@DispatchID", DbType.String, dispatchMissileOB.DispatchID);
                database.AddInParameter(dbCommand, "@SysNumber", DbType.String, dispatchMissileOB.SysNumber);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, dispatchMissileOB.MissileModel);
                database.AddInParameter(dbCommand, "@MissileCode", DbType.String, dispatchMissileOB.MissileCode);
                database.AddInParameter(dbCommand, "@InDate", DbType.String, dispatchMissileOB.InDate);
                database.AddInParameter(dbCommand, "@RemainingLife", DbType.String, dispatchMissileOB.RemainingLife);
                database.AddInParameter(dbCommand, "@IsCompleted", DbType.String, dispatchMissileOB.IsCompleted);
                database.AddInParameter(dbCommand, "@CompletionDate", DbType.String, dispatchMissileOB.CompletionDate);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DispatchMissile_Delete(string dispatchMissileID)
        {
            string sql = @"DELETE FROM dbo.t5715_DispatchMissile WHERE DispatchMissileID = @DispatchMissileID";
            //删除相关工序
            string sql2 = @"DELETE FROM dbo.t5715_DispatchMissileProcess WHERE DispatchMissileID = @DispatchMissileID";
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, dispatchMissileID);                
                database.ExecuteNonQuery(dbCommand);

                DbCommand dbCommand2 = database.GetSqlStringCommand(sql2);
                database.AddInParameter(dbCommand2, "@DispatchMissileID", DbType.String, dispatchMissileID);
                database.ExecuteNonQuery(dbCommand2);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DbCommand GetDbCommand_DispatchMissileProcess_Insert(Database database, DispatchMissileProcessOB dispatchMissileProcessOB)
        {
            string sql = @"INSERT INTO dbo.t5715_DispatchMissileProcess
                                   ([DispatchMissileProcessID]
                                   ,[DispatchMissileID]
                                   ,[ProcessID]
                                   ,[ProcessSortNumber]
                                   ,[SectionCode]
                                   ,[IsBeginRepair]
                                   ,[BeginRepairDate]
                                   ,[IsFault]
                                   ,[FaultReason]
                                   ,[IsCompleted]
                                   ,[CompletionDate]
                                   ,[InspectionState]
                                   ,[UnqualifiedType]
                                   ,[Remark])
                             VALUES
                                   (@DispatchMissileProcessID
                                   ,@DispatchMissileID
                                   ,@ProcessID
                                   ,@ProcessSortNumber
                                   ,@SectionCode
                                   ,@IsBeginRepair
                                   ,@BeginRepairDate
                                   ,@IsFault
                                   ,@FaultReason
                                   ,@IsCompleted
                                   ,@CompletionDate
                                   ,@InspectionState
                                   ,@UnqualifiedType
                                   ,@Remark)";

            try
            {
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, dispatchMissileProcessOB.DispatchMissileProcessID);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, dispatchMissileProcessOB.DispatchMissileID);
                database.AddInParameter(dbCommand, "@ProcessID", DbType.String, dispatchMissileProcessOB.ProcessID);
                database.AddInParameter(dbCommand, "@ProcessSortNumber", DbType.String, dispatchMissileProcessOB.ProcessSortNumber);
                database.AddInParameter(dbCommand, "@SectionCode", DbType.String, dispatchMissileProcessOB.SectionCode);
                database.AddInParameter(dbCommand, "@IsBeginRepair", DbType.String, dispatchMissileProcessOB.IsBeginRepair);
                database.AddInParameter(dbCommand, "@BeginRepairDate", DbType.String, dispatchMissileProcessOB.BeginRepairDate);
                database.AddInParameter(dbCommand, "@IsFault", DbType.String, dispatchMissileProcessOB.IsFault);
                database.AddInParameter(dbCommand, "@FaultReason", DbType.String, dispatchMissileProcessOB.FaultReason);
                database.AddInParameter(dbCommand, "@IsCompleted", DbType.String, dispatchMissileProcessOB.IsCompleted);
                database.AddInParameter(dbCommand, "@CompletionDate", DbType.String, dispatchMissileProcessOB.CompletionDate);
                database.AddInParameter(dbCommand, "@InspectionState", DbType.String, dispatchMissileProcessOB.InspectionState);
                database.AddInParameter(dbCommand, "@UnqualifiedType", DbType.String, dispatchMissileProcessOB.UnqualifiedType);
                database.AddInParameter(dbCommand, "@Remark", DbType.String, dispatchMissileProcessOB.Remark);

                return dbCommand;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }        

        public DispatchOB GetDispatchByID(string flowInstanceID)
        {
            string sql = @"SELECT * FROM dbo.t5715_Dispatch WHERE FlowInstanceID = @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                IDataReader reader = database.ExecuteReader(dbCommand);
                DispatchOB dispatchOB = null;
                if (reader.Read())
                {
                    dispatchOB = new DispatchOB();
                    dispatchOB.FlowInstanceID = reader["FlowInstanceID"] != null ? reader["FlowInstanceID"].ToString() : string.Empty;
                    dispatchOB.DispatchCode = reader["DispatchCode"] != null ? reader["DispatchCode"].ToString() : string.Empty;
                    dispatchOB.MissileModel = reader["MissileModel"] != null ? reader["MissileModel"].ToString() : string.Empty;
                    dispatchOB.RepairType = reader["RepairType"] != null ? reader["RepairType"].ToString() : string.Empty;
                    dispatchOB.PlanCompletionDate = reader["PlanCompletionDate"] != null ? reader["PlanCompletionDate"].ToString() : string.Empty;
                    dispatchOB.CreateUser = reader["CreateUser"] != null ? reader["CreateUser"].ToString() : string.Empty;
                    dispatchOB.CreateDate = reader["CreateDate"] != null ? reader["CreateDate"].ToString() : string.Empty;
                    dispatchOB.ModifyUser = reader["ModifyUser"] != null ? reader["ModifyUser"].ToString() : string.Empty;
                    dispatchOB.ModifyDate = reader["ModifyDate"] != null ? reader["ModifyDate"].ToString() : string.Empty;
                    dispatchOB.IssueState = reader["IssueState"] != null ? reader["IssueState"].ToString() : string.Empty;
                    dispatchOB.IssueUser = reader["IssueUser"] != null ? reader["IssueUser"].ToString() : string.Empty;
                    dispatchOB.IssueDate = reader["IssueDate"] != null ? reader["IssueDate"].ToString() : string.Empty;
                    dispatchOB.DispatchState = reader["DispatchState"] != null ? reader["DispatchState"].ToString() : string.Empty;
                    dispatchOB.DispatchCompletedDate = reader["DispatchCompletedDate"] != null ? reader["DispatchCompletedDate"].ToString() : string.Empty;
                    dispatchOB.Remark = reader["Remark"] != null ? reader["Remark"].ToString() : string.Empty;
                    
                    reader.Close();
                }

                return dispatchOB;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DispatchMissileProcess_Delete(string dispatchMissileProcessID)
        {
            string sql = @"DELETE FROM dbo.t5715_DispatchMissileProcess WHERE DispatchMissileProcessID = @DispatchMissileProcessID";
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, dispatchMissileProcessID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 派工单号是否已存在
        public bool HasDispatchCode(string dispatchCode, string flowInstanceID)
        {
            string sql = @"SELECT COUNT(FlowInstanceID) FROM dbo.t5715_Dispatch WHERE DispatchCode = @DispatchCode AND FlowInstanceID <> @FlowInstanceID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchCode", DbType.String, dispatchCode);
                database.AddInParameter(dbCommand, "@FlowInstanceID", DbType.String, flowInstanceID);
                object obj = database.ExecuteScalar(dbCommand);
                return obj.ToString().Equals("0") ? false : true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 获取派工单的导弹数据
        public DataTable GetDispatchMissileTable(string dispatchID)
        {
            string sql = @"SELECT * FROM dbo.t5715_DispatchMissile WHERE DispatchID = @DispatchID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchID", DbType.String, dispatchID);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 获取SBODB待修导弹数据
        public DataTable GetMissileTodoList(string missileModel, string level)
        {
            string sql = @"SELECT t1.itemCode, t1.distnumber, t1.InDate, t1.U_LifeTime,t1.SysNumber FROM osrq t0 
                            JOIN osrn t1 ON t0.sysnumber=t1.sysnumber AND t0.itemCode = t1.itemCode
                            JOIN OWHS T2 ON T2.WhsCode=t0.WhsCode
                            WHERE t0.quantity>0 AND T2.U_Class2=202 AND t0.itemCode=@MissileModel AND t1.U_Level=@level";

            string sql2 = @"SELECT A.DispatchMissileID, C.OperationID FROM dbo.t5715_DispatchMissile A
                                LEFT JOIN dbo.t5715_Dispatch B ON A.DispatchID = B.FlowInstanceID
                                LEFT JOIN dbo.FlowInstance C ON C.FlowInstanceID = B.FlowInstanceID
                                WHERE A.MissileModel=@MissileModel AND A.SysNumber=@SysNumber";

            try
            {
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, missileModel);
                database.AddInParameter(dbCommand, "@level", DbType.String, level);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];

                DataTable dataSource = new DataTable();
                dataSource.Columns.Add("ItemCode");
                dataSource.Columns.Add("DistNumber");
                dataSource.Columns.Add("SysNumber");
                dataSource.Columns.Add("InDate", typeof(DateTime));
                dataSource.Columns.Add("RemainingLife");

                database = DatabaseFactory.CreateDatabase();

                foreach (DataRow dataRow in dataTable.Rows)
                {
                    dbCommand = database.GetSqlStringCommand(sql2);
                    database.AddInParameter(dbCommand, "@MissileModel", DbType.String, dataRow["itemCode"].ToString());
                    database.AddInParameter(dbCommand, "@SysNumber", DbType.String, dataRow["SysNumber"].ToString());
                    using (IDataReader reader = database.ExecuteReader(dbCommand))
                    {
                        if (reader.Read())
                        {
                            string operationID = reader["OperationID"] == null ? string.Empty : reader["OperationID"].ToString();
                            if (operationID.Equals("4"))
                            {
                                dataSource.Rows.Add(dataRow["itemCode"], dataRow["distnumber"], dataRow["SysNumber"], dataRow["InDate"], dataRow["U_LifeTime"]);
                            }
                        }
                        else
                        {
                            dataSource.Rows.Add(dataRow["itemCode"], dataRow["distnumber"], dataRow["SysNumber"], dataRow["InDate"], dataRow["U_LifeTime"]);
                        }
                    }
                }

                return dataSource;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 根据导弹型号获取下发车间
        public DataTable GetIssueWorkShop(string missileModel)
        {
            string sql = @"SELECT DISTINCT B.* FROM dbo.t5715_Department A
                            INNER JOIN dbo.t5715_Department B ON A.ParentDeptID = B.DeptID
                            WHERE A.DeptID IN (SELECT DISTINCT DeptID FROM [dbo].[t5715_Process] WHERE MissileModel = @MissileModel)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, missileModel);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 根据导弹型号获取各工序负责的部门ID
        public DataTable GetDeptIDByMissileModel(string missileModel)
        {
            string sql = @"SELECT DeptID FROM dbo.t5715_Process WHERE MissileModel = @MissileModel";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, missileModel);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                return dataTable;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DataTable GetWastageInfo(string DispatchCode, string MissileCode, string ProcessSortNumber)
        {
            string sql = @"SELECT T0.ItemCode,T1.ItemName,T1.InvntryUom,T0.DistNumber,T0.LotNumber,SUM(T0.Quantity) AS Quantity                                FROM                                (								SELECT T1.ITEMCODE,-T3.QUANTITY QUANTITY,T4.DistNumber,T4.LotNumber                                FROM OIGE T0                                JOIN IGE1 T1 ON T0.DOCENTRY=T1.DOCENTRY                                JOIN OITL T2 ON T2.DocType=T0.ObjType AND T0.DocEntry=T2.DocEntry AND T1.LineNum=T2.DocLine AND T2.ManagedBy='10000045'                                JOIN ITL1 T3 ON T3.LogEntry=T2.LogEntry                                JOIN OSRN T4 ON T4.ItemCode=T3.ItemCode AND T4.SysNumber=T3.SysNumber                                WHERE T0.U_TRSNAME=201 AND T1.U_DISPATCHID=@U_SRCNUM  AND T1.U_MISSILECODE=@U_Missile AND T1.U_PROCESS=@U_Operation                                UNION ALL                                SELECT T1.ITEMCODE,T3.QUANTITY,T4.DistNumber,T4.LotNumber                                FROM OIGN T0                                JOIN IGN1 T1 ON T0.DOCENTRY=T1.DOCENTRY                                JOIN OITL T2 ON T2.DocType=T0.ObjType AND T0.DocEntry=T2.DocEntry AND T1.LineNum=T2.DocLine AND T2.ManagedBy='10000045'                                JOIN ITL1 T3 ON T3.LogEntry=T2.LogEntry                                JOIN OSRN T4 ON T4.ItemCode=T3.ItemCode AND T4.SysNumber=T3.SysNumber                                WHERE T0.U_TRSNAME=201 AND T1.U_DISPATCHID=@U_SRCNUM  AND T1.U_MISSILECODE=@U_Missile AND T1.U_PROCESS=@U_Operation                                                                UNION ALL								SELECT T1.ITEMCODE,-T3.QUANTITY QUANTITY,'',T4.DistNumber                                FROM OIGE T0                                JOIN IGE1 T1 ON T0.DOCENTRY=T1.DOCENTRY                                JOIN OITL T2 ON T2.DocType=T0.ObjType AND T0.DocEntry=T2.DocEntry AND T1.LineNum=T2.DocLine AND T2.ManagedBy='10000044'                                JOIN ITL1 T3 ON T3.LogEntry=T2.LogEntry                                JOIN OBTN T4 ON T4.ItemCode=T3.ItemCode AND T4.SysNumber=T3.SysNumber                                WHERE T0.U_TRSNAME=201 AND T1.U_DISPATCHID=@U_SRCNUM  AND T1.U_MISSILECODE=@U_Missile AND T1.U_PROCESS=@U_Operation                                                                UNION ALL                                  SELECT T1.ITEMCODE,T3.QUANTITY,'',T4.DistNumber                                FROM OIGN T0                                JOIN IGN1 T1 ON T0.DOCENTRY=T1.DOCENTRY                                JOIN OITL T2 ON T2.DocType=T0.ObjType AND T0.DocEntry=T2.DocEntry AND T1.LineNum=T2.DocLine AND T2.ManagedBy='10000044'                                JOIN ITL1 T3 ON T3.LogEntry=T2.LogEntry                                JOIN OBTN T4 ON T4.ItemCode=T3.ItemCode AND T4.SysNumber=T3.SysNumber                                WHERE T0.U_TRSNAME=201 AND T1.U_DISPATCHID=@U_SRCNUM  AND T1.U_MISSILECODE=@U_Missile AND T1.U_PROCESS=@U_Operation                                                                 UNION ALL                                                                SELECT T1.ITEMCODE,T1.QUANTITY,'',''                                FROM OIGE T0                                JOIN IGE1 T1 ON T0.DOCENTRY=T1.DOCENTRY                                JOIN OITM T2 ON T2.ITEMCODE=T1.ITEMCODE                                WHERE T2.MANBTCHNUM='N' AND T2.MANSERNUM='N' AND T0.U_TRSNAME=201 AND T1.U_DISPATCHID=@U_SRCNUM  AND T1.U_MISSILECODE=@U_Missile AND T1.U_PROCESS=@U_Operation                                                                UNION ALL                                                                SELECT T1.ITEMCODE,-T1.QUANTITY,'',''                                FROM OIGN T0                                JOIN IGN1 T1 ON T0.DOCENTRY=T1.DOCENTRY                                JOIN OITM T2 ON T2.ITEMCODE=T1.ITEMCODE                                WHERE T2.MANBTCHNUM='N' AND T2.MANSERNUM='N' AND T0.U_TRSNAME=201 AND T1.U_DISPATCHID=@U_SRCNUM  AND T1.U_MISSILECODE=@U_Missile AND T1.U_PROCESS=@U_Operation                                                                                ) T0                        JOIN OITM T1 ON T0.ItemCode=T1.ItemCode                        GROUP BY T0.ItemCode,T1.ItemName,T1.InvntryUom,T0.DistNumber,T0.LotNumber                        HAVING SUM(T0.QUANTITY)<>0";

            Database database = DatabaseFactory.CreateDatabase(Common.Helper.Instance.ConnectionStringName_SBODB);
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@U_SRCNUM", DbType.String, DispatchCode);
            database.AddInParameter(dbCommand, "@U_Missile", DbType.String, MissileCode);
            database.AddInParameter(dbCommand, "@U_Operation", DbType.String, ProcessSortNumber);

            return database.ExecuteDataSet(dbCommand).Tables[0];
        }
           
        /// <summary>
        /// 根据工序ID获取工序对象
        /// </summary>
        /// <param name="ProcessID"></param>
        /// <returns></returns>
        public ProcessOB GetProcessOBById(string ProcessID)
        {
            string sql = "select * from t5715_Process where ProcessID=@ProcessID";
            ProcessOB obj = null;
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ProcessID", DbType.String, ProcessID);
                using (IDataReader r = database.ExecuteReader(dbCommand))
                {
                    while (r.Read())
                    {
                        obj = new ProcessOB();
                        obj.ProcessID = r["ProcessID"].ToString();
                        obj.ProcessName = r["ProcessName"].ToString();
                        obj.MissileModel = r["MissileModel"].ToString();
                        obj.RepairType = r["RepairType"].ToString();
                        obj.DeptID = r["DeptID"].ToString();
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }

            return obj;
         

        }


        // 根据导弹型号获取第一步工序
        public string GetFirstProcessID(string missileModel, string RepairType)
        {
            string sql = @"SELECT ProcessID FROM dbo.t5715_Process WHERE MissileModel = @MissileModel and IsFirst=1";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@MissileModel", DbType.String, missileModel);
                object obj = database.ExecuteScalar(dbCommand);
                return obj == null ? string.Empty : obj.ToString();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DispatchMissileOB GetDispatchMissileById(string id)
        {
            string sql = "SELECT * FROM t5715_DispatchMissile WHERE (DispatchMissileID = @DispatchMissileID)";
            DispatchMissileOB obj = null;
            try
            {
              
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, id);
                using (IDataReader r = database.ExecuteReader(dbCommand))
                {
                    while (r.Read())
                    {
                        obj = new DispatchMissileOB();
                        obj.DispatchMissileID = r["DispatchMissileID"].ToString();
                        obj.DispatchID = r["DispatchID"].ToString();
                        obj.MissileModel = r["MissileModel"].ToString();
                        obj.MissileCode = r["MissileCode"].ToString();
                        obj.InDate = r["InDate"].ToString();
                        obj.RemainingLife = r["RemainingLife"].ToString();
                        obj.IsCompleted = r["IsCompleted"].ToString();
                        obj.CompletionDate = r["CompletionDate"].ToString();
                    }
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            return obj;
        }        

        public DispatchMissileProcessOB GetDispatchMissileProcessById(string DispatchMissileProcessID)
        {
            DispatchMissileProcessOB obj = null;
            string sql = @"SELECT * FROM t5715_DispatchMissileProcess WHERE DispatchMissileProcessID=@DispatchMissileProcessID";
            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, DispatchMissileProcessID);
            try
            {
                using (IDataReader r = database.ExecuteReader(dbCommand))
                {
                    while (r.Read())
                    {
                        obj = new DispatchMissileProcessOB();
                        obj.DispatchMissileProcessID = r["DispatchMissileProcessID"].ToString();
                        obj.DispatchMissileID = r["DispatchMissileID"].ToString();
                        obj.ProcessID = r["ProcessID"].ToString();
                        obj.ProcessSortNumber = r["ProcessSortNumber"].ToString();
                        obj.SectionCode = r["SectionCode"].ToString();
                        obj.IsBeginRepair = r["IsBeginRepair"].ToString();
                        obj.BeginRepairDate = r["BeginRepairDate"].ToString();

                        obj.IsFault = r["IsFault"].ToString();
                        obj.FaultReason = r["FaultReason"].ToString();
                        obj.IsCompleted = r["IsCompleted"].ToString();
                        obj.CompletionDate = r["CompletionDate"].ToString();
                        obj.InspectionState = r["InspectionState"].ToString();
                        obj.UnqualifiedType = r["UnqualifiedType"].ToString();
                        obj.Remark = r["Remark"].ToString();

                       
                    }
                }
            }
            catch (Exception)
            {
                
                throw;
            }
            return obj;
        }

        // 工序是否领修
        public bool IsBeginRepair(DispatchMissileProcessOB dispatchMissileProcessOB)
        {
            bool isBeginRepair = false;
            try
            {
                if (dispatchMissileProcessOB != null)
                { 
                    if(bool.TryParse(dispatchMissileProcessOB.IsBeginRepair, out isBeginRepair))
                    {
                        return isBeginRepair;
                    }
                }
                return isBeginRepair;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 工序是否完成
        public bool IsCompleted(DispatchMissileProcessOB dispatchMissileProcessOB)
        {
            bool isCompleted = false;
            try
            {
                if (dispatchMissileProcessOB != null)
                {
                    if (bool.TryParse(dispatchMissileProcessOB.IsCompleted, out isCompleted))
                    {
                        return isCompleted;
                    }
                }
                return isCompleted;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 查询派工单中是否还有未完成的导弹
        public bool hasUnCompletedInDispatchMissile(string flowInstanceID)
        {
            string sql = "SELECT COUNT(*) FROM [dbo].[t5715_DispatchMissile]WHERE IsCompleted = 0 AND DispatchID = @DispatchID";            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchID", DbType.String, flowInstanceID);
                object obj = database.ExecuteScalar(dbCommand);
                return obj.ToString().Equals("0") ? false : true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 检查导弹是否还有未完成或未检验的工序
        public bool hasUnCompletedInDispatchMissileProcess(string dispatchMissileID)
        {
            string sql = @"SELECT  COUNT(*) FROM dbo.t5715_DispatchMissileProcess
                              WHERE DispatchMissileID = @DispatchMissileID
	                            AND ((IsCompleted <> 1 OR IsCompleted IS NULL) OR  InspectionState IS NULL)";
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, dispatchMissileID);
                object obj = database.ExecuteScalar(dbCommand);
                return obj.ToString().Equals("0") ? false : true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 检查导弹是否在在修库。只有在在修库的导弹才可以编辑工序
        public bool InZaiXiuKu(string itemCode, string distNumber)
        {
            string sql = @"SELECT t1.itemCode, t1.distnumber FROM osrq t0 
                    JOIN osrn t1 ON t0.sysnumber=t1.sysnumber AND t0.itemCode = t1.itemCode
                    JOIN OWHS T2 ON T2.WhsCode=t0.WhsCode
                    WHERE t0.quantity>0 AND T2.U_Class2=205 AND t0.itemCode=@itemCode AND t1.distnumber = @distnumber";

            try
            {
                Database database = DatabaseFactory.CreateDatabase(Helper.Instance.ConnectionStringName_SBODB);
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@itemCode", DbType.String, itemCode);
                database.AddInParameter(dbCommand, "@distnumber", DbType.String, distNumber);
                DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        // 当前登录用户是否具有某个工序的检验权限
        public bool CanCheckProcess(string userID, string processDeptID)
        {
            string sql = @"SELECT COUNT(ID) FROM dbo.t5715_UserProcessCheckSettings WHERE UserID = @UserID AND DeptID = @DeptID";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@UserID", DbType.String, userID);
                database.AddInParameter(dbCommand, "@DeptID", DbType.String, processDeptID);
                object obj = database.ExecuteScalar(dbCommand);
                if (obj.ToString().Equals("0"))
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取所有工序，根据导弹维修派工单ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetDispatchMissileProcessByDispatchMissileID(string DispatchMissileID)
        {
            string sql = @"SELECT A.*,B.ProcessName, B.DeptID, D.DeptName + '(' + C.DeptName + ')' AS DeptName FROM [dbo].[t5715_DispatchMissileProcess] A
                            LEFT JOIN dbo.t5715_Process B ON A.ProcessID = B.ProcessID
                            LEFT JOIN dbo.t5715_Department C ON B.DeptID = C.DeptID
                            LEFT JOIN dbo.t5715_Department D ON C.ParentDeptID = D.DeptID
                            WHERE (A.DispatchMissileID=@DispatchMissileID) order by A.processsortNumber";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, DispatchMissileID);
                return database.ExecuteDataSet(dbCommand).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 设置工序检验标志位
        /// </summary>
        /// <param name="DispatchMissileProcessID"></param>
        public void ProcessCheck(string DispatchMissileProcessID, string InspectionState, string UnqualifiedType)
        {
            string sql = @"UPDATE    t5715_DispatchMissileProcess 
                           SET 
                               InspectionState = @InspectionState, 
                               UnqualifiedType = @UnqualifiedType
                           WHERE (DispatchMissileProcessID = @DispatchMissileProcessID)";

            try
            {
              
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@InspectionState", DbType.String, InspectionState);
                if (string.IsNullOrEmpty(UnqualifiedType))
                    database.AddInParameter(dbCommand, "@UnqualifiedType", DbType.String, DBNull.Value);
                else
                    database.AddInParameter(dbCommand, "@UnqualifiedType", DbType.String, UnqualifiedType);

   
                database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, DispatchMissileProcessID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 重新插入工序
        /// </summary>
        /// <param name="DispatchMissileProcessID"></param>
        public void ProcessReInsert(string DispatchMissileProcessID)
        {

            DispatchMissileProcessOB obj = GetDispatchMissileProcessById(DispatchMissileProcessID);

             int MaxOrder = GetMaxProcessOrderNumber(obj.DispatchMissileID)+1;



            string sql = @"INSERT INTO dbo.t5715_DispatchMissileProcess
                                   ([DispatchMissileProcessID]
                                   ,[DispatchMissileID]
                                   ,[ProcessID]
                                   ,[ProcessSortNumber]
                                   ,[IsBeginRepair])
                             VALUES
                                   (@DispatchMissileProcessID
                                   ,@DispatchMissileID
                                   ,@ProcessID
                                   ,@ProcessSortNumber
                                   ,@IsBeginRepair)";


            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, Guid.NewGuid().ToString());
            database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, obj.DispatchMissileID);
            database.AddInParameter(dbCommand, "@ProcessID", DbType.String, obj.ProcessID);
            database.AddInParameter(dbCommand, "@ProcessSortNumber", DbType.String, MaxOrder.ToString());
            database.AddInParameter(dbCommand, "@IsBeginRepair", DbType.String, "0");

            database.ExecuteNonQuery(dbCommand);


           
  


        }

        /// <summary>
        /// 清空工序状态信息
        /// </summary>
        /// <param name="DispatchMissileProcessID"></param>
        public void ProcessClear(string DispatchMissileProcessID)
        {
            string sql = @"UPDATE    t5715_DispatchMissileProcess 
                           SET 
                               SectionCode = NULL,
                               IsBeginRepair = 0,
                               BeginRepairDate = NULL,
                               IsFault = NULL,
                               FaultReason = NULL,
                               IsCompleted = NULL, 
                               CompletionDate = NULL,
                               InspectionState = NULL,
                               UnqualifiedType = NULL,
                               Remark = NULL
                           WHERE (DispatchMissileProcessID = @DispatchMissileProcessID)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, DispatchMissileProcessID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 将全部工序设置为完成状态
        /// </summary>
        /// <param name="DispatchMissileProcessID"></param>
        public void SetDispatchMissileComplete(string DispatchMissileID)
        {
            string sql = @"UPDATE t5715_DispatchMissile 
                           SET 
                               IsCompleted = 1, 
                               CompletionDate = GETDATE()
                           WHERE (DispatchMissileID = @DispatchMissileID)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, DispatchMissileID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 将工序设置为完成状态
        /// </summary>
        /// <param name="DispatchMissileProcessID"></param>
        public void SetProcessComplete(string DispatchMissileProcessID)
        {
            string sql = @"UPDATE    t5715_DispatchMissileProcess 
                           SET 
                               IsCompleted = 1, 
                               CompletionDate = GETDATE()
                           WHERE (DispatchMissileProcessID = @DispatchMissileProcessID)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileProcessID", DbType.String, DispatchMissileProcessID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        /// <summary>
        /// 获得工序列表中最大排序号
        /// </summary>
        /// <param name="DispatchMissileID"></param>
        /// <returns></returns>
        public int GetMaxProcessOrderNumber(string DispatchMissileID)
        {                                     
            string sql = @"select isnull(max(processSortNumber),0) from dbo.t5715_DispatchMissileProcess 
                           where DispatchMissileID =@DispatchMissileID ";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, DispatchMissileID);
                object MaxNum = database.ExecuteScalar(dbCommand);

                return Convert.ToInt32(MaxNum);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        #region 串件信息

        /// <summary>
        /// 获取ExchangePartOb列表
        /// </summary>
        /// <param name="DispatchMissileProcessRouteID"></param>
        /// <returns></returns>
        public List<ExchangePartOB> GetExchangePartObList(string DispatchMissileProcessRouteID)
        {
            string sql = "select * from [t5715_ExchangePart] where [DispatchMissileProcessRouteID] = @DispatchMissileProcessRouteID order by ExchangeDate desc";
            List<ExchangePartOB> result = null; 
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@DispatchMissileProcessRouteID", DbType.String, DispatchMissileProcessRouteID);
                using (IDataReader r = database.ExecuteReader(dbCommand))
                {
                    result = new List<ExchangePartOB>();
                    while (r.Read())
                    {
                        ExchangePartOB obj = new ExchangePartOB();
                        obj.ExchangePartID = r["ExchangePartID"].ToString();
                        obj.DispatchMissileProcessRouteID = r["DispatchMissileProcessRouteID"].ToString();
                        obj.Source_DispatchCode = r["Source_DispatchCode"].ToString();
                        obj.Source_MissileCode = r["Source_MissileCode"].ToString();
                        obj.Source_PartCode = r["Source_PartCode"].ToString();
                        obj.Source_PartNumber = r["Source_PartNumber"].ToString();
                        obj.Source_WorkTeam = r["Source_WorkTeam"].ToString();
                        obj.Targe_DispatchCode = r["Targe_DispatchCode"].ToString();
                        obj.Targe_MissileCode = r["Targe_MissileCode"].ToString();
                        obj.Targe_PartCode = r["Targe_PartCode"].ToString();
                        obj.Targe_PartNumber = r["Targe_PartNumber"].ToString();
                        obj.Targe_WorkTeam = r["Targe_WorkTeam"].ToString();
                        obj.ExchangeDate = Convert.ToDateTime(r["ExchangeDate"]);

                        result.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

            return result;
        }

        /// <summary>
        /// 根据ID获得ExchangePartOB
        /// </summary>
        /// <param name="ExchangePartID"></param>
        /// <returns></returns>
        public ExchangePartOB GetExchangePartObById(string ExchangePartID)
        {
            string sql = "select * from [t5715_ExchangePart] where [ExchangePartID] = @ExchangePartID";


            ExchangePartOB obj = null;
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ExchangePartID", DbType.String, ExchangePartID);
                using (IDataReader r = database.ExecuteReader(dbCommand))
                {
                    while (r.Read())
                    {
                        obj = new ExchangePartOB();
                        obj.ExchangePartID = r["ExchangePartID"].ToString();
                        obj.DispatchMissileProcessRouteID = r["DispatchMissileProcessRouteID"].ToString();
                        obj.Source_DispatchCode = r["Source_DispatchCode"].ToString();
                        obj.Source_MissileCode = r["Source_MissileCode"].ToString();
                        obj.Source_PartCode = r["Source_PartCode"].ToString();
                        obj.Source_PartNumber = r["Source_PartNumber"].ToString();
                        obj.Source_WorkTeam = r["Source_WorkTeam"].ToString();
                        obj.Targe_DispatchCode = r["Targe_DispatchCode"].ToString();
                        obj.Targe_MissileCode = r["Targe_MissileCode"].ToString();
                        obj.Targe_PartCode = r["Targe_PartCode"].ToString();
                        obj.Targe_PartNumber = r["Targe_PartNumber"].ToString();
                        obj.Targe_WorkTeam = r["Targe_WorkTeam"].ToString();
                        obj.ExchangeDate = Convert.ToDateTime(r["ExchangeDate"]);
                    }
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }


            return obj;

        }

        public void ExchangePartOB_Insert(ExchangePartOB obj)
        {
            string sql = @"INSERT INTO [t5715_ExchangePart]
                           ([ExchangePartID]
                           ,[DispatchMissileProcessRouteID]
                           ,[Source_DispatchCode]
                           ,[Source_MissileCode]
                           ,[Source_WorkTeam]
                           ,[Source_PartCode]
                           ,[Source_PartNumber]
                           ,[Targe_DispatchCode]
                           ,[Targe_MissileCode]
                           ,[Targe_WorkTeam]
                           ,[Targe_PartCode]
                           ,[Targe_PartNumber]
                           ,[ExchangeDate])
                     VALUES
                           (@ExchangePartID
                           ,@DispatchMissileProcessRouteID
                           ,@Source_DispatchCode
                           ,@Source_MissileCode
                           ,@Source_WorkTeam
                           ,@Source_PartCode
                           ,@Source_PartNumber
                           ,@Targe_DispatchCode
                           ,@Targe_MissileCode
                           ,@Targe_WorkTeam
                           ,@Targe_PartCode
                           ,@Targe_PartNumber
                           ,@ExchangeDate)";

            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ExchangePartID", DbType.String, obj.ExchangePartID);
                database.AddInParameter(dbCommand, "@DispatchMissileProcessRouteID", DbType.String, obj.DispatchMissileProcessRouteID);
                database.AddInParameter(dbCommand, "@Source_DispatchCode", DbType.String, obj.Source_DispatchCode);
                database.AddInParameter(dbCommand, "@Source_MissileCode", DbType.String, obj.Source_MissileCode);
                database.AddInParameter(dbCommand, "@Source_WorkTeam", DbType.String, obj.Source_WorkTeam);
                database.AddInParameter(dbCommand, "@Source_PartCode", DbType.String, obj.Source_PartCode);
                database.AddInParameter(dbCommand, "@Source_PartNumber", DbType.String, obj.Source_PartNumber);
                database.AddInParameter(dbCommand, "@Targe_DispatchCode", DbType.String, obj.Targe_DispatchCode);
                database.AddInParameter(dbCommand, "@Targe_MissileCode", DbType.String, obj.Targe_MissileCode);
                database.AddInParameter(dbCommand, "@Targe_WorkTeam", DbType.String, obj.Targe_WorkTeam);
                database.AddInParameter(dbCommand, "@Targe_PartCode", DbType.String, obj.Targe_PartCode);
                database.AddInParameter(dbCommand, "@Targe_PartNumber", DbType.String, obj.Targe_PartNumber);
                database.AddInParameter(dbCommand, "@ExchangeDate", DbType.DateTime, obj.ExchangeDate);

                database.ExecuteNonQuery(dbCommand);
           ;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }
        public void ExchangePartOB_Update(ExchangePartOB obj)
        {
            string sql = @"UPDATE [t5715_ExchangePart]
                           SET 
                               [Source_DispatchCode] = @Source_DispatchCode
                              ,[Source_MissileCode] = @Source_MissileCode
                              ,[Source_WorkTeam] = @Source_WorkTeam
                              ,[Source_PartCode] = @Source_PartCode
                              ,[Source_PartNumber] = @Source_PartNumber
                              ,[Targe_DispatchCode] = @Targe_DispatchCode
                              ,[Targe_MissileCode] = @Targe_MissileCode
                              ,[Targe_WorkTeam] = @Targe_WorkTeam
                              ,[Targe_PartCode] = @Targe_PartCode
                              ,[Targe_PartNumber] = @Targe_PartNumber
                         WHERE [ExchangePartID] = @ExchangePartID";


            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ExchangePartID", DbType.String, obj.ExchangePartID);
  
                database.AddInParameter(dbCommand, "@Source_DispatchCode", DbType.String, obj.Source_DispatchCode);
                database.AddInParameter(dbCommand, "@Source_MissileCode", DbType.String, obj.Source_MissileCode);
                database.AddInParameter(dbCommand, "@Source_WorkTeam", DbType.String, obj.Source_WorkTeam);
                database.AddInParameter(dbCommand, "@Source_PartCode", DbType.String, obj.Source_PartCode);
                database.AddInParameter(dbCommand, "@Source_PartNumber", DbType.String, obj.Source_PartNumber);
                database.AddInParameter(dbCommand, "@Targe_DispatchCode", DbType.String, obj.Targe_DispatchCode);
                database.AddInParameter(dbCommand, "@Targe_MissileCode", DbType.String, obj.Targe_MissileCode);
                database.AddInParameter(dbCommand, "@Targe_WorkTeam", DbType.String, obj.Targe_WorkTeam);
                database.AddInParameter(dbCommand, "@Targe_PartCode", DbType.String, obj.Targe_PartCode);
                database.AddInParameter(dbCommand, "@Targe_PartNumber", DbType.String, obj.Targe_PartNumber);


                database.ExecuteNonQuery(dbCommand);
                
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public void ExchangePartOB_Delete(string ExchangePartID)
        {
            string sql = @"DELETE FROM [t5715_ExchangePart] where ExchangePartID=@ExchangePartID";
            try
            {
                 Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetSqlStringCommand(sql);
                database.AddInParameter(dbCommand, "@ExchangePartID", DbType.String,ExchangePartID);
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        #endregion

    }
}